<template>
  <el-dialog class="chartDialog" :visible.sync="dialogVisible">
    <template v-if="isMoreChartTypes">
      <el-radio-group v-model="chartType" style="display: table; margin: 0 auto;" size="small">
        <el-radio-button v-for="type in chartTypes" :label="type.label" :key="type.label">{{type.text}}
        </el-radio-button>
      </el-radio-group>
    </template>
    <template v-if="haveSearchObj">
      <normal-form :inline="true"
                   :searchBtn="true"
                   :showLabel="true"
                   :data-form="chartSearchObj.dataForm"
                   :config="chartSearchObj.config"
                   :submitFunc="commitChartSearch"
      ></normal-form>
    </template>
    <div :style="chartContainerStyle()" :key="Math.random()">
      <slot name="chart" v-bind:chart={chartType}></slot>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import normalForm from '@/components/normalForm/form'

export default {
  name: 'index',
  components: {normalForm},
  props: {
    chartTypes: {
      default: () => []
    },
    chartSearchObj: {
      default: () => ({})
    },
    commitChartSearch: {
      default: () => {
      }
    }
  },
  computed: {
    haveSearchObj() {
      const chartSearchObjDataForm = this.R.prop('dataForm', this.chartSearchObj)

      if (this.R.isNil(chartSearchObjDataForm)) {
        return false
      }
      return Object.keys(chartSearchObjDataForm).length > 0
    },
    isMoreChartTypes() {
      return this.chartTypes instanceof Array && this.chartTypes.length > 0
    }
  },
  data() {
    return {
      dialogVisible: false,
      chartType: this.R.path([0, 'label'], this.chartTypes)
    }
  },
  methods: {
    chartContainerStyle() {
      return {
        width: '100%',
        height: `calc(100% - ${this.isMoreChartTypes ? '32px' : '0px'} - ${this.haveSearchObj ? '62px' : '0px'})`
      }
    },
    openChartDialog(option) {
      this.dialogVisible = true
      this.$nextTick(() => {
        this.commitChartSearch(option)
      })
    }
  },
  mounted() {
  }
}
</script>

<style scoped>

</style>
